
import Draw from './Draw.js'
const DEF_STYLE ={
    material: Cesium.Color.YELLOW.withAlpha(0.6),
    fill: true
}
export default class PolygonDraw extends Draw{
    constructor(viewer,eventsManager,style){
        super(viewer, eventsManager);
        this._positions =[];
        this._style ={
            ...DEF_STYLE
        }
    }

    _mountEntity(){
        this._delegate =new Cesium.Entity({
            polygon:{
                ...this._style,
                hierarchy: new Cesium.CallbackProperty(()=>{
                    if(this._positions.length>2){
                        return new Cesium.PolygonHierarchy(this._positions)
                    }else{
                        return null
                    }
                },false),
                
            }
        })
        this._viewer.entities.add(this._delegate);
    }

    _onClick(e){
        let position =this._clampToGround ? e.surfacePosition : e.position ;
        let len =this._positions.length;
        if(len ===0){
            this._positions.push(position)
            this._floatingAnchor =this.createAnchor(position)
        }
        this._positions.push(position);
    }

    _onRightClick(e){
        this.unbindEvent();
        this._event.raiseEvent(this._positions);
        this.removeAnchor(this._floatingAnchor);
    }

    _onMouseMove(e){
        if(this._floatingAnchor){
            let position =this._clampToGround ? e.surfacePosition : e.position
            this._floatingAnchor.position.setValue(position);
            this._positions.pop();
            this._positions.push(position)
        }

    }
} 